package thesis.filesharing.web.controller;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import thesis.filesharing.bo.StudentBO;
import thesis.filesharing.common.util.XMLUtil;
import thesis.filesharing.model.Student;
import thesis.filesharing.resources.Constants;
import thesis.filesharing.web.validator.RegisterValidator;
import thesis.filesharing.web.viewmodel.RegisterViewModel;

@Controller
@RequestMapping(value = "/register")
public class RegisterController {
	
	@Autowired
	private RegisterValidator registerValidator;
	
	@Autowired
	private StudentBO studentBO;
	
	List<String> classNames = XMLUtil.readDataNodes(Constants.DEPARTMENTS_FILE_PATH, "department-name");
	List<String> courseNames = XMLUtil.readDataNodes(Constants.COURSES_FILE_PATH, "course");
	
	@PreAuthorize("!isAuthenticated()")
	@RequestMapping(method = RequestMethod.GET)
	public String initRegisterForm(ModelMap model) {
		RegisterViewModel registerViewModel = new RegisterViewModel();
		model.addAttribute("registerViewModel", registerViewModel);
		return "register";
	}
	
	@ModelAttribute("classes")
	public Map<Integer, String> populateClasses() {
		Map<Integer, String> classes = new LinkedHashMap<Integer, String>();
		for (int i = 0; i < classNames.size(); i++) {
			classes.put(i, classNames.get(i));
		}
		return classes;
	}
	
	@ModelAttribute("courses")
	public Map<Integer, Integer> populateCourses() {
		Map<Integer, Integer> courses = new LinkedHashMap<Integer, Integer>();
		for (int i = 0; i < courseNames.size(); i++) {
			courses.put(i, Integer.parseInt(courseNames.get(i)));
		}
		return courses;
	}
	
	@PreAuthorize("!isAuthenticated()")
	@RequestMapping(method = RequestMethod.POST)
	public String processRegisterSubmit(
			@ModelAttribute("registerViewModel") RegisterViewModel registerViewModel, 
			BindingResult bindingResult, SessionStatus status) {
		
		registerValidator.validate(registerViewModel, bindingResult);
		
		if (bindingResult.hasErrors()) {
			return "register";
		} else {
			status.setComplete();
			
			int classIndex = Integer.parseInt(registerViewModel.getClassName());
			int courseIndex = Integer.parseInt(registerViewModel.getCourse());
			String className = classNames.get(classIndex);
			int courseName = Integer.parseInt(courseNames.get(courseIndex));
			
			Student student = new Student(registerViewModel.getStudentId(), registerViewModel.getStudentName(), 
					registerViewModel.getStudentEmail(), className, courseName, registerViewModel.getPassword(), true);
			studentBO.saveStudent(student);
			
			// Originally, isActive set to false. After register, system will request to confirm using mail
			// After clicking to link in mail, isActive will set to true
			// Insert mail service code here
			
			return "registerSuccess";
		}
	}
}
